package com.prs.crm.action.doc;

import java.io.File;
import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.Util;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.doc.ShareUserDoc;
import com.prs.crm.domain.doc.UserDoc;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.doc.ShareUserDocService;
import com.prs.crm.service.doc.UserDocService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class UserDocAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private UserDoc userDoc;
	private UserDocService userDocService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private ShareUserDoc shareUserDoc;
	private ShareUserDocService shareUserDocService;

	private String sharePersons;
	private String sharePersonNames;

	private Collection<ShareUserDoc> shareUserDocs;
	private Date shareBeginTime;
	private Date shareEndTime;

	private File upload;
	private String uploadFileName;
	private String typeIds;

	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;

	private Collection<CodeCommon> codelist;

	@Action("listUserDoc")
	public String execute() {
		return SUCCESS;
	}

	@Action("listUserDocByType")
	public String listUserDocByType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				UserDoc.class);
		criteria.add(Restrictions.in("type", this.getUserDocService()
				.convertToDocType(typeIds)));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getUserDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editUserDoc")
	public String editUserDoc() {
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"我的文档");
		return SUCCESS;
	}

	@Action("viewUserDoc")
	public String viewUserDoc() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveUserDoc", results = {
			@Result(name = "success", type = "redirect", location = "editUserDoc"),
			@Result(name = "error", type = "chain", location = "editUserDoc") })
	public String saveUserDoc() {
		if (userDoc.getName() == null || "".equals(userDoc.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (userDoc.getUserDoccode() == null
				|| "".equals(userDoc.getUserDoccode().trim())) {
			addActionError("编码不能为空");
			return ERROR;
		}
		if ("n".equals(Encode)) {
			int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码",
					"我的文档", userDoc.getUserDoccode());
			if (EncodeRplice != 0) {
				this.addActionError("编码重复!请重新输入");
				return ERROR;
			}
		}
		if (userDoc.getType() == null) {
			addActionError("文档类型不能为空");
			return ERROR;
		}
		if ("n".equals(Encode)) {
			Code codes = new Code();
			codes.setCodeValue(userDoc.getUserDoccode());
			codes.setEntityName(userDoc.getClass().getName());
			codes.setCodeType("手动编码");
			codes.setCodeMolde("我的文档");
			this.getCodeService().save(codes);
			userDoc.setCode(codes);

		} else {
			CodeCommon Vob = this.getCodeCommonService().getVobject(
					codeCommon.getId());
			Code codes = new Code();
			codes.setEntityName(userDoc.getClass().getName());
			codes.setCodeValue(userDoc.getUserDoccode());
			Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
			this.codeCommonService.saveOrUpdate(Vob);
			codes.setCodeType(Vob.getCodePlan());
			codes.setCodeMolde("我的文档");
			this.getCodeService().save(codes);
			userDoc.setCode(codes);

		}
		this.getUserDocService().saveUserDoc(userDoc, upload, uploadFileName);
		this.setPrompt("提交文档成功");

		return SUCCESS;
	}

	@Action(value = "removeUserDoc", results = { @Result(name = "success", type = "redirect", location = "listUserDoc") })
	public String removeUserDoc() {
		this.getUserDocService().batchRemove(ids);
		return SUCCESS;
	}

	@Action("listShareUserDoc")
	public String listShareUserDoc() {
		return SUCCESS;
	}

	@Action("listShareUserDocByType")
	public String listShareUserDocByType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				UserDoc.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.eq("recorder", AppContext.getAccountContext()
				.currentUser()));
		if (typeIds != null) {
			if (!"all".equals(typeIds)) {
				criteria.add(Restrictions.in("type", this.getUserDocService()
						.convertToDocType(typeIds)));
			}
		}
		this.setPagination(this.getUserDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editShareUserDoc")
	public String editShareUserDoc() {
		shareUserDocs = this.getShareUserDocService().getShareDoc(userDoc);
		this.sharePersons = null;
		this.sharePersonNames = null;
		for (ShareUserDoc doc : shareUserDocs) {
			this.shareBeginTime = doc.getBeginTime();
			this.shareEndTime = doc.getEndTime();

			sharePersonNames = (sharePersonNames == null) ? doc.getPerson()
					.getResume().getName() : sharePersonNames + ","
					+ doc.getPerson().getResume().getName();
			sharePersons = (sharePersons == null) ? doc.getPerson().getId()
					.toString() : sharePersons + ","
					+ doc.getPerson().getId().toString();
		}
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "submitShareUserDoc", results = {
			@Result(name = "success", type = "redirect", location = "listShareUserDoc"),
			@Result(name = "error", type = "chain", location = "editShareUserDoc") })
	public String submitShareUserDoc() {
		if (shareUserDoc.getBeginTime() == null) {
			addActionError("开始不能为空");
			return ERROR;
		}
		if (shareUserDoc.getEndTime() == null) {
			addActionError("共享结束时间不能为空");
			return ERROR;
		}
		if (sharePersons == null || "".equals(sharePersons.trim())) {
			addActionError("共享人员不能为空");
			return ERROR;
		}
		this.setPrompt("提交文档共享成功");
		shareUserDoc.setUserDoc(userDoc);
		this.getShareUserDocService().saveShareUserDoc(shareUserDoc,
				sharePersons);
		return SUCCESS;
	}

	@Action("listMyAcquireShare")
	public String listMyAcquireShare() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ShareUserDoc.class);
		criteria.add(Restrictions.eq("person", AppContext.getAccountContext()
				.currentUser()));
		criteria.add(Restrictions.le("beginTime", Util.nowFullTime()));// beginTime小于等于当前时间
		criteria.add(Restrictions.ge("endTime", Util.nowFullTime()));// endTime大于等于当前时间
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getShareUserDocService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public UserDoc getUserDoc() {
		return userDoc;
	}

	public void setUserDoc(UserDoc userDoc) {
		this.userDoc = userDoc;
	}

	public UserDocService getUserDocService() {
		return userDocService;
	}

	public void setUserDocService(UserDocService userDocService) {
		this.userDocService = userDocService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public ShareUserDoc getShareUserDoc() {
		return shareUserDoc;
	}

	public void setShareUserDoc(ShareUserDoc shareUserDoc) {
		this.shareUserDoc = shareUserDoc;
	}

	public ShareUserDocService getShareUserDocService() {
		return shareUserDocService;
	}

	public void setShareUserDocService(ShareUserDocService shareUserDocService) {
		this.shareUserDocService = shareUserDocService;
	}

	public String getSharePersons() {
		return sharePersons;
	}

	public void setSharePersons(String sharePersons) {
		this.sharePersons = sharePersons;
	}

	public String getSharePersonNames() {
		return sharePersonNames;
	}

	public Date getShareBeginTime() {
		return shareBeginTime;
	}

	public void setShareBeginTime(Date shareBeginTime) {
		this.shareBeginTime = shareBeginTime;
	}

	public Date getShareEndTime() {
		return shareEndTime;
	}

	public void setShareEndTime(Date shareEndTime) {
		this.shareEndTime = shareEndTime;
	}

	public void setSharePersonNames(String sharePersonNames) {
		this.sharePersonNames = sharePersonNames;
	}

	public void setShareUserDocs(Collection<ShareUserDoc> shareUserDocs) {
		this.shareUserDocs = shareUserDocs;
	}

	public Collection<ShareUserDoc> getShareUserDocs() {
		return shareUserDocs;
	}

	public void setTypeIds(String typeIds) {
		this.typeIds = typeIds;
	}

	public String getTypeIds() {
		return typeIds;
	}

}
